package com.changpay.zywy.ui.activity.mine

import android.content.Intent
import android.support.v7.widget.LinearLayoutManager
import com.chad.library.adapter.base.BaseQuickAdapter
import com.changpay.zywy.R
import com.changpay.zywy.adapter.MyPolicyAdapter
import com.changpay.zywy.app.App.Companion.context
import com.changpay.zywy.base.BaseActivity
import com.changpay.zywy.base.IView
import com.changpay.zywy.ext.sss
import com.changpay.zywy.http.RetrofitHelper
import com.changpay.zywy.bean.MyMachineBean
import com.changpay.zywy.ui.activity.machinecontrol.FenRunActivity
import com.changpay.zywy.wight.SpaceItemDecoration
import kotlinx.android.synthetic.main.activity_machine_control.*
import kotlinx.android.synthetic.main.title_right_layout.*
import java.util.HashMap

class MyPolicyActivity : BaseActivity(), IView {
    override fun showLoading() {
    }

    override fun hideLoading() {

    }

    override fun showDefaultMsg(msg: String) {
    }

    override fun showMsg(msg: String) {
    }

    override fun showError(errorMsg: String) {
    }

    override fun attachLayoutRes(): Int {
        return R.layout.activity_recycle
    }

    override fun initData() {
    }

    override fun initView() {
        tv_title.text = "我的政策"
        iv_back.setOnClickListener{finish()}
        initRecyclerView()
    }

    override fun start() {
        loadData()
    }

    private fun loadData() {
        // 加载底部数据
        val params = HashMap<String, String>()
        params["client"] = "android"
        params["key"] = token
        datas.clear()
        RetrofitHelper.service.myMachine(params).sss(this) {
            it.result.let { it1 -> datas.addAll(it1.list!!) }
            mAdapter.notifyDataSetChanged()
        }

    }


    /**
     * datas
     */
    private val datas = mutableListOf<MyMachineBean.ListBean>()

    /**
     * RecyclerView Divider
     */
    private val recyclerViewItemDecoration by lazy {
        this.let {
            SpaceItemDecoration(context!!)
        }
    }

    /**
     * Adapter
     */
    private val mAdapter: MyPolicyAdapter by lazy {
        MyPolicyAdapter(context, datas)
    }


    /**
     * LinearLayoutManager
     */
    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(context)
    }


    /**
     * ItemClickListener
     */
    private val ItemClickListener = BaseQuickAdapter.OnItemClickListener { _, _, position ->
        val intent = Intent(this, FenRunActivity::class.java)
        intent.putExtra("goods_id", datas[position].goods_id.toString())
        intent.putExtra("others_member_id", "")
        intent.putExtra("title", "分润细则")
        startActivity(intent)
    }


    /**
     * ItemChildClickListener
     */
    private val onItemChildClickListener = BaseQuickAdapter.OnItemChildClickListener { _, view, position ->
        when(view.id)
        {

        }
    }

    private fun initRecyclerView() {
        recyclerView.run {
            layoutManager = linearLayoutManager
            adapter = mAdapter
//            itemAnimator = DefaultItemAnimator()
//            recyclerViewItemDecoration.let { addItemDecoration(it) }
        }
        mAdapter.run {
            bindToRecyclerView(recyclerView)
            onItemChildClickListener = this@MyPolicyActivity.onItemChildClickListener

//            setOnLoadMoreListener(onRequestLoadMoreListener, recyclerView)
           onItemClickListener = this@MyPolicyActivity.ItemClickListener
        }
    }

}